package h0909;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 字符串相加
 * @date 2024/9/9 17:08
 */
public class StringAdd {
    public static void main(String[] args) {
        String a = "9";
        String b = "99";
        System.out.println(addStrings(a, b));
    }

    public static String addStrings(String num1, String num2) {
        int carry = 0;
        StringBuilder res = new StringBuilder();
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        while (i >= 0 || j >= 0||carry!=0) {
            int a = i >= 0 ? num1.charAt(i) - '0' : 0;
            int b = j >= 0 ? num2.charAt(j) - '0' : 0;
            int sum = a + b + carry;

            res.append(sum % 10);
            carry = sum / 10;

            i--;
            j--;
        }

        return res.reverse().toString();
    }
}
